/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.yami.shop.service.PayService;
import com.yami.shop.service.utils.WeChatPayHelper;
import com.yami.shop.service.utils.WeChatPaymentNotifyRes;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/notice/pay")
@AllArgsConstructor
public class PayNoticeController {

    @Autowired
    private final PayService payService;

    @Autowired
    private WeChatPayHelper weChatPayHelper;

    @RequestMapping("/order")
    public ResponseEntity<Void> submit(HttpServletRequest request)  {

         System.out.println("3333333333");

        try {
            WeChatPaymentNotifyRes res=weChatPayHelper.validateSign(request);
            if(res.isValidateSign()){
                String payNo = res.getOutTradeNo();
                String bizPayNo = res.getTransactionId();
                payService.paySuccess(payNo, bizPayNo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.ok().build();
    }
}